package com.kyzh.core.pager.home

import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.FragmentTransaction
import com.gushenge.atools.util.AView
import com.gushenge.core.base.activity.BaseActivity
import com.gushenge.core.dao.GlobalKeys
import com.gushenge.core.dao.GlobalKeys.TYPE
import com.gushenge.core.dao.MMKVConsts
import com.gushenge.core.toast
import com.jeremyliao.liveeventbus.LiveEventBus
import com.kyzh.core.MyApplication
import com.kyzh.core.R
import com.kyzh.core.dialog.showMainView
import com.kyzh.core.pager.gamelist.GameFragment
import com.kyzh.core.pager.me.MeFragment
import com.kyzh.core.pager.me.user.ScanLoginActivity
import com.kyzh.core.pager.weal.WealFragment
import com.kyzh.core.pager.weal.quanzi.QuanZiFragment
import com.kyzh.core.utils.UpdateApp
import com.kyzh.core.utils.startGameDetailActivity
import com.shuyu.gsyvideoplayer.GSYVideoManager
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : BaseActivity() {

    private var wealFragment: WealFragment? = null
    private var homeFragment: HomeFragment? = null
    private var meFragment: MeFragment? = null
    private var videoFragment: QuanZiFragment? = null
    private var gameFragment: GameFragment? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        test()
    }
    private fun initView() {
        tabSelection(0)
        val intent = intent
        intent?.let {
            it.getStringExtra(TYPE)?.let {type->
                when(type){
                    GlobalKeys.GAMEACTIVITY ->{
                        val id = it.getStringExtra("id")
                        startGameDetailActivity(id)
                    }
                    else->{}
                }
            }
        }

        navigation.itemIconTintList = null
        // 头部颜色控制
        LiveEventBus.get<String>("QuanZiTitle").observe(this){
            if(it == "视频"){
                AView.setStatusBar(this,false)
                navigation.setBackgroundColor(Color.parseColor("#333333"))
                window.navigationBarColor = Color.parseColor("#333333")
            }else{
                AView.setStatusBar(this,true)
                navigation.setBackgroundColor(Color.WHITE)
                window.navigationBarColor = Color.WHITE
            }
        }
        navigation.setOnNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.home ->
                    tabSelection(0)
                R.id.sort -> tabSelection(1)
                R.id.video -> tabSelection(2)
                R.id.function -> tabSelection(3)
                R.id.me -> tabSelection(4)
            }
            true
        }
        UpdateApp().updateMain(this@MainActivity){
            showMainView()
        }

    }
    /*根据传入的index参数来设置选中的tab页。*/
    public fun tabSelection(index: Int) {
        GSYVideoManager.onPause()
        navigation.menu.getItem(index).isChecked = true
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)
        when (index) {
            0 ->{
                AView.setStatusBar(this,true)
                navigation.setBackgroundColor(Color.WHITE)
                window.navigationBarColor = Color.WHITE
                if (homeFragment == null) {
                    homeFragment = HomeFragment()
                    homeFragment?.let{
                        transaction.add(R.id.content,it )
                    }
                } else {
                    homeFragment?.let {
                        transaction.show(it)
                    }

                }
            }
            1 ->{
                AView.setStatusBar(this,true)
                navigation.setBackgroundColor(Color.WHITE)
                window.navigationBarColor = Color.WHITE
                if (gameFragment == null) {
                    gameFragment = GameFragment()
                    gameFragment?.let{
                        transaction.add(R.id.content, it)
                    }

                } else {
                    gameFragment?.let {
                        transaction.show(it)
                    }
                }
            }
            2 ->{
                LiveEventBus.get<Int>("QuanZi").post(0)
                AView.setStatusBar(this,true)
                navigation.setBackgroundColor(Color.WHITE)
                window.navigationBarColor = Color.WHITE
                if (videoFragment == null) {
                    videoFragment = QuanZiFragment()
                    videoFragment?.let{
                        transaction.add(R.id.content,it )
                    }
                } else {
                    videoFragment?.let{
                        transaction.show(it)
                        videoFragment?.onResume()
                    }
                }
            }
            3 ->{

                AView.setStatusBar(this,false)
                navigation.setBackgroundColor(Color.WHITE)
                window.navigationBarColor = Color.WHITE
                if (wealFragment == null) {
                    wealFragment = WealFragment()
                    wealFragment?.let{
                        transaction.add(R.id.content,it )
                    }

                } else {
                    wealFragment?.let{
                        transaction.show(it)
                    }
                }
            }

            4 ->{
                AView.setStatusBar(this, true)
                navigation.setBackgroundColor(Color.WHITE)
                window.navigationBarColor = Color.WHITE
                if (meFragment == null) {
                    meFragment = MeFragment()
                    meFragment?.let{
                        transaction.add(R.id.content,it )
                    }
                } else {
                    meFragment?.let{
                        transaction.show(it)
                    }
                }

            }

        }
        transaction.commit()
    }

    fun test(){
//        if(packageName == "a94hwan.bjkyzh.star"){
//            if(MMKVConsts.gogid.isNotEmpty()){
//                startGameDetailActivity(MMKVConsts.gogid)
//                MMKVConsts.gogid = ""
//            }
//        }


//        HttpQuery().ceshi("https://pro.94wan.com/?ct=app&ac=cceshi").enqueue(object: Callback<String>{
//            override fun onResponse(call: Call<String>, response: Response<String>) {
//                Log.e("TAG", "onResponse: ", )
//
//            }
//
//            override fun onFailure(call: Call<String>, t: Throwable) {
//            }
//
//        })
//        RxLifeScope().launch {
//            val result = RxHttp.get()
//                .toClass<String>()
//                .await()
//            Log.e("TAG", "test: "+result )
//        }
//

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode !== RESULT_OK || android.R.attr.data == null) {
            return
        }
        if (requestCode === 1000) {
//            val obj: HmsScan? =  data?.getParcelableExtra(ScanUtil.RESULT)

        }
    }


    /**
     * 将所有的Fragment都置为隐藏状态。
     * @param transaction 用于对Fragment执行操作的事务
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        homeFragment?.let{ transaction.hide(it) }
        gameFragment?.let{ transaction.hide(it) }
        wealFragment?.let{ transaction.hide(it) }
        videoFragment?.let{
            videoFragment?.onPause()
            transaction.hide(it)
        }
        meFragment?.let{ transaction.hide(it) }

    }
    /*返回键点击事件*/
    private var backPressedTime = System.currentTimeMillis()
    override fun onBackPressed() {
        if(GSYVideoManager.isFullState(this)){
            GSYVideoManager.backFromWindowFull(this)
        }else{
            homeFragment?:tabSelection(0)
            homeFragment?.let{
                if (it.isHidden) {
                    tabSelection(0)
                } else {
                    val nowTime = System.currentTimeMillis()
                    if (nowTime - backPressedTime < 1500) {
                        finishAffinity()
                    } else {
                        backPressedTime = nowTime
                        getString(R.string.doubleTapToExit).toast()
                    }
                }
            }
        }


    }

    override fun onPause() {
        super.onPause()
        GSYVideoManager.onPause()
    }

    override fun onRestart() {
        super.onRestart()
        GSYVideoManager.onPause()
    }

    override fun onResume() {
        super.onResume()
        GSYVideoManager.onPause()
    }
    fun me(){
        tabSelection(4)
    }


    fun quanzi(selectType  :Int){
        MyApplication.quanSelect = selectType
        tabSelection(2)
    }

    fun fuli(){
        tabSelection(3)
    }

}
